<script setup>
import { onMounted, ref, watch, nextTick } from 'vue'
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'
import { ElMessage } from 'element-plus'
import 'highlight.js/styles/github-dark.css'

const props = defineProps({
  content: {
    type: String,
    required: true
  }
})

const renderedContent = ref('')

const md = new MarkdownIt({
  html: true,
  linkify: true,
  typographer: true,
  highlight: function (str, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        const highlighted = hljs.highlight(str, { language: lang }).value
        return `<div class="code-block-wrapper">
                  <div class="code-block-header">
                    <span class="code-lang">${lang}</span>
                    <button class="copy-button" title="复制代码">
                      <svg viewBox="0 0 24 24" class="copy-icon">
                        <path fill="currentColor" d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/>
                      </svg>
                    </button>
                  </div>
                  <pre><code class="hljs ${lang}">${highlighted}</code></pre>
                </div>`
      } catch (__) {}
    }
    return ''
  }
})

// 使用 watch 来即时更新渲染内容
watch(() => props.content, (newContent) => {
  renderedContent.value = md.render(newContent || '')
}, { immediate: true })

const copyCode = async (code) => {
  try {
    await navigator.clipboard.writeText(code)
    ElMessage({
      message: '代码复制成功',
      type: 'success',
      duration: 2000,
      offset: 60
    })
  } catch (err) {
    console.error('Failed to copy:', err)
    ElMessage({
      message: '复制失败，请重试',
      type: 'error',
      duration: 2000,
      offset: 60
    })
  }
}

onMounted(() => {
  // 为每个代码块添加复制功能
  const updateCodeBlockListeners = () => {
    document.querySelectorAll('.code-block-wrapper .copy-button').forEach((button) => {
      button.addEventListener('click', (e) => {
        e.stopPropagation()
        const codeBlock = button.closest('.code-block-wrapper').querySelector('code')
        copyCode(codeBlock.textContent)
      })
    })
  }
  
  // 初始化时添加监听器
  updateCodeBlockListeners()
  
  // 监听内容变化时更新监听器
  watch(() => props.content, () => {
    nextTick(() => {
      updateCodeBlockListeners()
    })
  })
})
</script>

<template>
  <div class="markdown-body" v-html="renderedContent"></div>
</template>

<style>
.markdown-body {
  color: var(--text-primary);
  line-height: 1.6;
}

.code-block-wrapper {
  position: relative;
  margin: 0.15rem 0;
  border-radius: 0.25rem;
  background: var(--secondary-bg);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.code-block-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.2rem 0.75rem;
  background: rgba(255, 255, 255, 0.03);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  min-height: 24px;
}

.code-lang {
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-family: ui-monospace, 'Fira Code', monospace;
  text-transform: lowercase;
  opacity: 0.7;
}

.copy-button {
  display: flex;
  align-items: center;
  padding: 0.15rem 0.4rem;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 0.2rem;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.8rem;
  transition: all 0.15s ease;
  opacity: 0.7;
}

.copy-button:hover {
  background: rgba(255, 255, 255, 0.05);
  border-color: rgba(255, 255, 255, 0.2);
  color: var(--text-primary);
  opacity: 1;
}

.copy-icon {
  width: 0.8rem;
  height: 0.8rem;
}

.markdown-body pre {
  margin: 0;
  padding: 0.3rem 0.75rem;
  background: transparent;
  overflow-x: auto;
}

.markdown-body pre code {
  padding: 0;
  background: transparent;
  font-family: ui-monospace, 'Fira Code', monospace;
  font-size: 0.85rem;
  line-height: 1.4;
  white-space: pre;
}

/* 代码高亮样式优化 */
.hljs {
  background: transparent !important;
  padding: 0 !important;
}

.hljs-keyword,
.hljs-function {
  color: #ff79c6;
}

.hljs-string {
  color: #f1fa8c;
}

.hljs-number {
  color: #bd93f9;
}

.hljs-comment {
  color: #6272a4;
  font-style: italic;
  opacity: 0.8;
}

.hljs-variable,
.hljs-literal {
  color: #50fa7b;
}

/* 其他 Markdown 样式优化 */
.markdown-body p {
  margin: 0.25em 0;
  line-height: 1.5;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin-top: 1em;
  margin-bottom: 0.5em;
  font-weight: 600;
  line-height: 1.3;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 1.2em;
  margin: 0.5em 0;
}

.markdown-body li {
  margin: 0.2em 0;
}

.markdown-body blockquote {
  border-left: 2px solid var(--border-color);
  margin: 0.25em 0;
  padding: 0.2em 0.75em;
  color: var(--text-secondary);
  background: rgba(255, 255, 255, 0.02);
  border-radius: 0.2rem;
}

/* 滚动条美化 */
.markdown-body pre::-webkit-scrollbar {
  height: 3px;
}

.markdown-body pre::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 1.5px;
}

.markdown-body pre::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* 代码行间距优化 */
.markdown-body pre code {
  display: block;
  padding: 0;
}

/* 行内代码样式 */
.markdown-body code:not(.hljs) {
  background: rgba(255, 255, 255, 0.05);
  padding: 0.15em 0.3em;
  border-radius: 0.2rem;
  font-size: 0.85em;
  color: var(--text-primary);
}

/* 移除重复的样式 */
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5em 0;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border-color);
  padding: 0.4em;
}
</style> 